package com.money.care;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

public class FunctionActivity extends Activity {
	// Debug
	final String TAG = "FunctionActivity";
	final boolean D = true;
	
	// SharedPreferences
	public static final String PREF = "CARE_PREF"; // 設定檔
	public static final String PREF_LOG = "CARE_LOG";
	SharedPreferences settings;
	boolean login_state;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// Remove title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (D)
			Log.w(TAG, "Remove title");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_funtion);
		
		settings = getSharedPreferences(PREF, 0);
		login_state=settings.getBoolean(PREF_LOG, false);
		
		// Check login state
		if(!login_state)
		{
			this.finish();
			Intent i =new Intent();
			i.setClass(FunctionActivity.this, LoginActivity.class);
			startActivity(i);
		}
	}

	@Override
    protected void onDestroy()
	{ 
        super.onDestroy();
		settings.edit().putBoolean(PREF_LOG, false).commit();
		
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
		mNotificationManager.cancelAll();
		
		settings.edit()
		.putBoolean(PREF_LOG, false)
		.commit();
    }
	
	/**
	 * Button onClick
	 * 
	 * @param view get button id
	 */
	public void OnClick(View view) {
		switch (view.getId()) {
		case R.id.imageButton_function_contact:
			onContact();
			break;
		case R.id.imageButton_function_dial:
			onDial();
			break;
		case R.id.imageButton_function_injure:
			onInjure();
			break;
		case R.id.imageButton_function_picture:
			onGallery();
			break;
		case R.id.imageButton_function_setting:
			onSetting();			
			break;
		case R.id.imageButton_function_track:
			onTrack();
			break;
		}
	}

	private void onGallery() {
		// TODO Auto-generated method stub
		Intent i =new Intent();
		i.setClass(this, GalleryActivity.class);
		if (D)
			Log.w(TAG, "Change to gallery activity");
		startActivity(i);
	}

	private void onInjure() {
		// TODO Auto-generated method stub
		Intent i =new Intent();
		i.setClass(this, InjureActivity.class);
		if (D)
			Log.w(TAG, "Change to injure activity");
		startActivity(i);
	}

	private void onDial() {
		// TODO Auto-generated method stub
		Intent i =new Intent();
		i.setClass(this, CallActivity.class);
		if (D)
			Log.w(TAG, "Change to call activity");
		startActivity(i);
	}

	private void onContact() {
		// TODO Auto-generated method stub
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("聯絡資訊");
		dialog.setMessage(
				  "組長：吳姿誼           \naz1252002@yahoo.com.tw\n\n"
				+"組員：林婉蒨           \nb79544@yahoo.com.tw\n\n"
				+"      	阮冠翔            \ncliffpkcliff@yahoo.com.tw\n\n"
				+"    		張伊雯           \nshes0502030@yahoo.com.tw\n\n"
				+"    		江柏宏            \nangus315@yahoo.com.tw\n\n"
				+"			凃德彬           \nqoo800800@yahoo.com.tw\n\n"
				+"			郭子豪           \njason80819@yahoo.com.tw");
		dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		dialog.show();
	}

	/**
	 * Go to track page
	 */
	private void onTrack() {
		// TODO Auto-generated method stub
		Intent i =new Intent();
		i.setClass(this, TrackActivity.class);
		if (D)
			Log.w(TAG, "Change to track activity");
		startActivity(i);
	}

	/**
	 * Go to setting page
	 */
	private void onSetting() {
		// TODO Auto-generated method stub
		Intent i =new Intent();
		i.setClass(this, SettingActivity.class);
		if (D)
			Log.w(TAG, "Change to setting activity");
		startActivity(i);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	        Log.d(this.getClass().getName(), "back button pressed");
	        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle("離開");
			dialog.setMessage(
					  "是否離開程式?");
			dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					//android.os.Process.killProcess(android.os.Process.myPid());
					settings.edit()
					.putBoolean(PREF_LOG, false)
					.commit();
					
					String ns = Context.NOTIFICATION_SERVICE;
					NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
					mNotificationManager.cancelAll();
					
					int pid = android.os.Process.myPid();
					android.os.Process.killProcess(pid);
				}
			});
			dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					return;
				}
			});
			dialog.show();
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
